//
//  ZBLinkmanViewC.m
//  BaseRACDemo
//
//  Created by Angel_Yan on 2018/3/27.
//  Copyright © 2018年 庄波. All rights reserved.
//

#import "ZBLinkmanViewC.h"

#import "ZBLinkmanTableViewCell.h"
#import "ZBLinkmanItemViewModel.h"
#import "AFFAlertView.h"

@interface ZBLinkmanViewC ()
/// 返回按钮
@property (nonatomic, readwrite, strong) UIBarButtonItem *backItem;
@property (nonatomic, weak) UIButton *btnRight;
@property (nonatomic, readwrite, strong) ZBLinkmanViewModel *viewModel;

@end

@implementation ZBLinkmanViewC

- (void)viewDidLoad {
    /// ①：子类调用父类的viewDidLoad方法，而父类主要是创建tableView以及强行布局子控件，从而导致tableView刷新，这样就会去走tableView的数据源方法
    [super viewDidLoad];
    /// 设置
    [self _setup];
    /// ③：注册cell
    [self.tableView mh_registerNibCell:[ZBLinkmanTableViewCell class]];
    

}

- (void)bindViewModel {
    [super bindViewModel];
}


#pragma mark - Override
/// 配置tableView的区域
- (UIEdgeInsets)contentInset{
    return UIEdgeInsetsMake(SafeAreaTopHeight, 0, SafeAreaTabbarHeight, 0);
}

/// 返回自定义的cell
- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath{
    // ②：父类的tableView的数据源方法的获取cell是通过注册cell的identifier来获取cell，然而此时子类并未注册cell，所以取出来的cell = nil而引发Crash
    return [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZBLinkmanTableViewCell class]) forIndexPath:indexPath];
}

/// 绑定数据
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
    ZBLinkmanTableViewCell *linkNameCell = (ZBLinkmanTableViewCell *)cell;
    [linkNameCell bindViewModel:object];
}

#pragma mark - 初始化
- (void)_setup{
    /// set up ...
    self.navigationItem.leftBarButtonItem = self.backItem;

    UIButton *btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    btnRight.frame = CGRectMake(0, 0, ItemWidth, kNavigationBar_Height);
    [btnRight setImage:[UIImage imageNamed:@"juxingxuankuang"] forState:UIControlStateNormal];
    [btnRight setImage:[UIImage imageNamed:@"juxiangxuankuang_gouxuan"] forState:UIControlStateSelected];
    [btnRight setContentVerticalAlignment: UIControlContentVerticalAlignmentCenter];
    [btnRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnRight];
    self.btnRight = btnRight;
    
    ///< 全选
    [[self.btnRight rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIButton * _Nullable x) {
        x.selected = !x.isSelected;
        NSArray *arr = [[self.viewModel.dataSource rac_sequence] map:^id _Nullable(ZBLinkmanItemViewModel *_Nullable value) {
            value.isSelect = x.selected;
            return value;
        }].array;
        if (x.isSelected == YES) {
            self.viewModel.arrSelect = arr;
        } else {
            self.viewModel.arrSelect = nil;
        }
    }];
    
    ///< 判断是否为全选
    [RACObserve(self, viewModel.isAllSelect) subscribeNext:^(id  _Nullable x) {
        self.btnRight.selected = [x boolValue];
    }];
}

- (UIBarButtonItem *)backItem
{
    if (_backItem == nil) {
        _backItem = [UIBarButtonItem mh_backItemWithTitle:@"返回" imageName:@"barbuttonicon_back_15x30" target:self action:@selector(pullBack)];
    }
    return _backItem;
}

- (void)cleanSlef {
    self.viewModel = nil;
}

- (void)pullBack {
    NSString *str = [NSString stringWithFormat:@"当前选中人数:%ld",self.viewModel.arrSelect.count];
    [AFFAlertView alertWithTitle:str btnTitle:@[@"取消",@"确定"] block:^(NSInteger index, BOOL isCancel) {
        [AFFAlertView dismiss];
        if (index == 0) {
        } else if (index == 1) {
            if (self.viewModel.selectLinkman) {
                NSMutableArray *mArr = [[NSMutableArray alloc] init];
                for (ZBLinkmanItemViewModel *itemViewModel in self.viewModel.arrSelect) {
                    [mArr addObject:itemViewModel.linkmanItem.strPhone];
                }
                self.viewModel.selectLinkman(mArr);
            }
        }
        [super pullBack];
    }];
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellheight = [self.viewModel.dataSource[indexPath.row] cellHeight];
    return cellheight;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
